package com.chuiyuan.db;

import org.apache.ibatis.io.Resources;
import org.apache.ibatis.session.SqlSessionFactory;
import org.apache.ibatis.session.SqlSessionFactoryBuilder;
import org.apache.ibatis.session.defaults.DefaultSqlSessionFactory;

import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.InputStream;
import java.io.Reader;

/**
 * Created by chuiyuan on 3/21/16.
 */
public class ConnectionFactory {
    private static SqlSessionFactory sqlSessionFactory ;
    static {
        try {
            String resource ="config/mybatis-config.xml";
            InputStream inputStream = Resources.getResourceAsStream(resource);
            if ( sqlSessionFactory== null){
                sqlSessionFactory= new SqlSessionFactoryBuilder().build(inputStream);
            }
        }catch (FileNotFoundException fe ){
            fe.printStackTrace();
        }
        catch (IOException ioe){
            ioe.printStackTrace();
        }
    }

    public static SqlSessionFactory getSqlSessionFactory(){
        return sqlSessionFactory ;
    }
}
